package ch.subgate.spritestudio.widgets;

import ch.subgate.spritestudio.constants.Constants;
import ch.subgate.spritestudio.tools.OnColorChangedListener;
import ch.subgate.spritestudio.types.RGBAListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RGBAPresenterView extends View implements RGBAListener {
	
	private int mColor;
	
	private int mOldColor;
	
	/** Paint object to draw the overlaying color */
	private Paint mColorPaint;
	
	/** Paint object to draw the background pattern */
	private Paint mPatternPaint;
	
	/** Paint object to draw the boundaries */
	private Paint mBoundsPaint;
	
	private OnColorChangedListener mListener;

	public RGBAPresenterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mColor = 0xff000000;
		mOldColor = 0xff000000;
		
		mColorPaint = new Paint();
		mPatternPaint = new Paint();
		mBoundsPaint = new Paint();
		
		mListener = null;
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		//Draw the background pattern
		for (int i = 0; i * 36 < this.getMeasuredWidth(); i++) {
			for (int j = 0; j * 36 < this.getMeasuredHeight(); j++) {
				if ((i + j) % 2 == 0) {
					mPatternPaint.setColor(0xffaaaaaa);
					canvas.drawRect(
							(float)(i * 36),
							(float)(j * 36),
							(float)((i + 1) * 36),
							(float)((j + 1) * 36),
							mPatternPaint);
				}
				else {
					mPatternPaint.setColor(0xffcccccc);
					canvas.drawRect(
							(float)(i * 36),
							(float)(j * 36),
							(float)((i + 1) * 36),
							(float)((j + 1) * 36),
							mPatternPaint);
				}
			}
		}
		
		//Draw the overlaying color
		mColorPaint.setColor(mOldColor);
		canvas.drawRect(
				0.0f,
				0.0f,
				((float)this.getMeasuredWidth()) / 2.0f,
				(float)this.getMeasuredHeight(),
				mColorPaint);
		mColorPaint.setColor(mColor);
		canvas.drawRect(
				((float)this.getMeasuredWidth()) / 2.0f,
				0.0f,
				(float)this.getMeasuredWidth(),
				(float)this.getMeasuredHeight(),
				mColorPaint);
		
		//Draw the borders
		mBoundsPaint.setColor(0x66ffffff);
		canvas.drawLine(0.0f, 0.0f, this.getMeasuredWidth(), 0.0f, mBoundsPaint);
		canvas.drawLine(0.0f, 0.0f, 0.0f, this.getMeasuredHeight(), mBoundsPaint);
		mBoundsPaint.setColor(0x66000000);
		canvas.drawLine(0.0f, this.getMeasuredHeight() - 1.0f, this.getMeasuredWidth(), this.getMeasuredHeight() - 1.0f, mBoundsPaint);
		canvas.drawLine(this.getMeasuredWidth() - 1.0f, 0.0f, this.getMeasuredWidth() - 1.0f, this.getMeasuredHeight(), mBoundsPaint);
	}

	@Override
	public void onRGBAValueChanged(int value, int relevancy) {
		switch (relevancy) {
		case Constants.COLOR_A_RELEVANT:
			mColor = (mColor & 0x00ffffff) | (value << Constants.COLOR_A_OFFSET);
			break;
		case Constants.COLOR_R_RELEVANT:
			mColor = (mColor & 0xff00ffff) | (value << Constants.COLOR_R_OFFSET);
			break;
		case Constants.COLOR_G_RELEVANT:
			mColor = (mColor & 0xffff00ff) | (value << Constants.COLOR_G_OFFSET);
			break;
		case Constants.COLOR_B_RELEVANT:
			mColor = (mColor & 0xffffff00) | (value << Constants.COLOR_B_OFFSET);
			break;
		}
		onColorChanged();
		invalidate();
	}
	
	public void setColor(int color) {
		mColor = color;
		onColorChanged();
		invalidate();
	}
	
	public int getColor() {
		return mColor;
	}
	
	public void setOldColor(int color) {
		mOldColor = color;
		invalidate();
	}
	
	public int getOldColor() {
		return mOldColor;
	}
	
	public void onColorChanged() {
		if (mListener != null) {
			mListener.onColorChanged(mColor);
		}
	}
	
	public void setOnColorChangedListener(OnColorChangedListener listener) {
		mListener = listener;
	}

}
